<html>
<body>
<title>Test that XMLHttpRequest.response returns an empty ArrayBuffer when received a response without an entity body.</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script>

var zeroContentLengthTest = async_test("Test that XMLHttpRequest.response returns an empty ArrayBuffer when received a response with zero content length.");
zeroContentLengthTest.step(function() {
    var req = new XMLHttpRequest;
    req.responseType = 'arraybuffer';
    req.open('GET', 'resources/zero-length.xml', true);
    req.onreadystatechange = zeroContentLengthTest.step_func(function() {
        if (req.readyState != 4)
            return;

        assert_equals(req.status, 200);
        assert_equals(Object.prototype.toString.call(req.response), '[object ArrayBuffer]');
        assert_equals(req.response.byteLength, 0);
        zeroContentLengthTest.done();
    });
    req.send(null);
});


var notModifiedTest = async_test("Test that XMLHttpRequest.response returns an empty ArrayBuffer when received status is '304 not modified'.");
notModifiedTest.step(function() {
    var req = new XMLHttpRequest;
    req.responseType = 'arraybuffer';
    req.open('GET', 'resources/reply.xml', true);
    req.onreadystatechange = notModifiedTest.step_func(function() {
        if (req.readyState != 4)
            return;

        assert_equals(req.status, 200);
        assert_equals(Object.prototype.toString.call(req.response), '[object ArrayBuffer]');
        assert_equals(req.response.byteLength, 68);

        var lastModified = req.getResponseHeader("Last-Modified");

        var req2 = new XMLHttpRequest;
        req2.responseType = 'arraybuffer';
        req2.open('GET', 'resources/reply.xml', true);
        req2.setRequestHeader("If-Modified-Since", lastModified);
        req2.onreadystatechange = notModifiedTest.step_func(function() {
            if (req2.readyState != 4)
                return;

            assert_equals(req2.status, 304);
            assert_equals(Object.prototype.toString.call(req2.response), '[object ArrayBuffer]');
            assert_equals(req2.response.byteLength, 0);
            notModifiedTest.done();
        });
        req2.send(null);
    });
    req.send(null);
});

</script>
</body>
